package com.UBC417.termproject.Servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.UBC417.termproject.Data.Seller;

@SuppressWarnings("serial")
public class CreateAuctionServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(SubscribeServlet.class.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		// Get parameters		
		String itemDesc = req.getParameter("itemDesc");
		String itemBP = req.getParameter("itemBP");
		String itemET = req.getParameter("itemET");
		String EThour = req.getParameter ("EThour");
		String ETampm = req.getParameter("ETampm");
		
		if (ETampm.equals("pm")){
			Double t = Double.parseDouble(EThour);
			t = t+12;
			EThour = t.toString();
		}
		
		HttpSession session = req.getSession();
		String userId = (String) session.getAttribute("id");
		
		//String forwardTo = "/?.jsp";
				
		log.info("itemDesc: "+ itemDesc);
		log.info ("itemBP: "+ itemBP);
		log.info ("itemET: "+ itemET);
		log.info ("EThour: "+ EThour);
		log.info ("ETampm: "+ ETampm);
		
		String dateStr = itemET+'/'+EThour;
		try {
			Date start = new Date();
			Date end = new SimpleDateFormat("dd/MM/yyyy/h", Locale.ENGLISH).parse(dateStr);
			Seller.CreateAuction(itemDesc, itemDesc, itemBP, userId, start, end, "Open");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// redirect to user page
//        String forwardTo = "/seller.jsp";
//		ServletContext sc = getServletContext();
//		RequestDispatcher rd = sc.getRequestDispatcher(forwardTo);
//		rd.forward(req, resp);
		resp.sendRedirect("/seller.jsp");
		
	}	

}
